package cn.bdqn.controller;

import cn.bdqn.utils.httputil.FTPUtil;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import cn.bdqn.utils.httputil.UrlUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 文件上传控制层
 * @author dkj
 */
@RestController
@RequestMapping("/upload")
public class UploadFileController {



    /**
     * 上传单个文件的方法
     * @param request
     * @param file 图片
     * @return
     */
    @RequestMapping("/singleFile")
    public Result singleUploadImage(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        try {

            // 获得原始名称
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.indexOf("."));
            //使用UUId加工图片名
            String processingImageName = UUID.randomUUID().toString()+suffix;
            //获得项目名
            String context = request.getContextPath();

            byte [] bytes = file.getBytes();
            // 文件上传
            FTPUtil.sshSftp(bytes, processingImageName,"image");
            //返回路径
            return ResultGenerator.genSuccessResult(UrlUtil.LINUX_URL+"/image/"+processingImageName);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("上传失败");
        }
    }


    /**
     * 上传单个文件的方法
     * @param request
     * @param files 图片合集
     * @return
     */
    @RequestMapping("/multipleFile")
    public Result multipleUploadImage(HttpServletRequest request, @RequestParam("file") MultipartFile[] files){

        try {
            // 1、得到文件上传的路径
            String path = request.getServletContext().getRealPath("/results/");
            File destPath = new File(path);
            if(!destPath.exists()){
                destPath.mkdirs();
            }
            List urls = new ArrayList();
            for (MultipartFile file:files) {
                // 获得原始名称
                String originalFilename = file.getOriginalFilename();
                String suffix = originalFilename.substring(originalFilename.indexOf("."));
                //使用UUId加工图片名
                String processingImageName = UUID.randomUUID().toString()+suffix;
                //获得项目名
                String context = request.getContextPath();

                byte [] bytes = file.getBytes();
                // 文件上传
                FTPUtil.sshSftp(bytes, processingImageName,"image");

                //获得文件路径
                String url = UrlUtil.LINUX_URL+"/image/"+processingImageName;
                //保存路径数组
                urls.add(url);
            }
            //返回路径数组
            return ResultGenerator.genSuccessResult(urls);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("上传失败");
        }
    }

}
